<template>
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    title="资产类型选择"
    style="min-width: 1400px"
    draggable
    @close="closeDialog"
    destroy-on-close
  >
    <!-- <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="量检具类型" prop="assetTypeId">
        
          <el-tree-select
            placeholder="请选择量检具类型"
            v-model="searchParam.assetTypeId"
            :data="catalogList"
            node-key="id"
            :props="{
              label: 'assetTypeName',
              value: 'id',
              children: 'nodes',
            }"
            :render-after-expand="false"
            :check-strictly="true"
            clearable
          />
        </el-form-item>

        
        </el-form-item>
        <el-form-item label="量检具名称">
          <el-input
            v-model="searchParam.assetName"
            placeholder="请输入量检具名称"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="产品" prop="productId">
          <el-select
            v-model="searchParam.productId"
            placeholder="请选择产品"
            clearable
          >
            <el-option
              v-for="item in productist"
              :key="`${item.id}`"
              :label="`${item.productCode}-${item.productName}`"
              :value="`${item.id}`"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="位置">
          <el-tree-select
            placeholder="请输入位置"
            filterable
            v-model="searchParam.locationId"
            :data="locationInfoTree"
            node-key="id"
            clearable
            :props="{ label: 'locationName', children: 'nodes' }"
            check-strictly
            :render-after-expand="false"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div> -->
    <el-card shadow="never" class="table-container">
      <div class="formTable1">
        <el-table
          ref="singleTableRef"
          v-loading="loading"
          :data="tableData"
          highlight-current-row
          border
          @selection-change="handleCurrentChange1"
          row-key="id"
          :indent="30"
          default-expand-all
          :tree-props="{
            children: 'nodes',
            hasChildren: 'hasChildren',
          }"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column
            label="设备种类编号"
            prop="assetTypeCode"
            min-width="100"
            show-overflow-tooltip
          />
          <el-table-column
            label="设备种类名称"
            prop="assetTypeName"
            min-width="500"
            show-overflow-tooltip
          />

          <el-table-column
            label="排序"
            prop="sort"
            min-width="100"
            show-overflow-tooltip
            align="center"
          />

          <el-table-column
            label="启用状态"
            prop="enabled"
            min-width="100"
            show-overflow-tooltip
            align="center"
          >
            <template #default="scope">
              <el-tag type="success" v-if="scope.row.enabled === 1"
                >启用</el-tag
              >
              <el-tag type="info" v-else>停用</el-tag>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" :loading="loading" @click="handleSubmit"
          >确 定
        </el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { useTable, useDictionary } from "@/utils/useTable";
import { ElMessage, ElForm } from "element-plus";
import { ref, reactive, watch, onBeforeUnmount } from "vue";
import request from "@/utils/request";
import dayjs from "dayjs";
defineOptions({
  name: "COMMONASSETTYPESELECT",
  inheritAttrs: false,
});

const emit = defineEmits(["change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  LocationList: {
    type: Object,
    default: () => {
      return {};
    },
  },
  assetKind: {
    type: Number,
    default: null,
  },
});

//获取表格数据 api 方法
const requestApi = "/eam/eamCommon/getTree";
//获取数据初始化参数
const initParam = { assetKind: props.assetKind };
// 初始化搜索参数数据
// 初始化搜索参数数据
const searchParamInit = {};
// 是否需要分页
const pagination = true;
// 无需二次数据处理&不传赞数也行
const tableData1 = ref([]);
const dataCallBack = (data: any) => {
  // data.forEach((item: any, index: number) => {
  // });
  // if (data) {
  //   data = data.filter(function (value, index, arr) {
  //     return value.assetStatus == "3";
  //   });
  // }
  return data;
};
// const dataCallBack = ref(undefined);
// 数据请求错误后处理
function requestError() {}
//删除
const deleteParam = {
  api: "",
  data: {},
};

const {
  tableData,
  pageable,
  ids,
  searchParam,
  reset,
  search,
  getTableList,
  handleSizeChange,
  handleSelectionChange,
  handleTableDelete,
  handleCurrentChange,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack,
  requestError,
  deleteParam
);

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    breakSingleTableSelect();
  }
);

// 选择高亮
const singleTableRef = ref();
const currentRow = ref([]);
const handleCurrentChange1 = (val: any) => {
  currentRow.value = val;
};

// 回显时设置选项
const breakSingleTableSelect = () => {
  setChildrenSelection(tableData.value, props.LocationList);
};

// 递归设置子节点的选中状态
const defaultCheckedKeys = ref([]);
function setChildrenSelection(node, selectedKeys) {
  node.forEach((item) => {
    selectedKeys.forEach((element) => {
      if (element.id == item.id) {
        nextTick(() => {
          //遍历回显数组
          //singleTableRef这个是在table上面加的ref的值  通过它的属性方法去看那几个属性打钩
          singleTableRef.value.toggleRowSelection(item, true); // 回显
        });
      }
    });
    if (item.nodes && item.nodes.length > 0) {
      setChildrenSelection(item.nodes, selectedKeys);
    }
  });
}
/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
}

/** email消息发送记录保存提交 */
function handleSubmit() {
  if (currentRow.value.length > 0) {
    emit("change-visible", false, currentRow.value);
  } else {
    return ElMessage.warning("请选择至少一项资产类型");
  }
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});

onMounted(() => {
  getTableList();
  // 获取位置信息下拉树
});
</script>

<style scoped lang="scss">
:deep(.el-table th .cell) {
  color: black;
  font-weight: 700;
}
.progress {
  color: red;
}
.success {
  color: rgb(100, 207, 86);
}
</style>
